﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace JqlEditor
{
    internal class JqlChessman
    {
        public static readonly JqlChessman Empty;
        static JqlChessman()
        {
            Empty = new JqlChessman
            {
                ChessmanType = ChessmanType.None,
                Visible = false
            };
        }

        public ChessmanType ChessmanType { get; set; }
        public bool Visible { get; set; }

        public JqlChessman()
        {
            Visible = true;
        }
        public JqlChessman(ChessmanType type)
        {
            ChessmanType = type;
            Visible = type != ChessmanType.None;
        }

        public bool IsEmpty()
        {
            return Equals(JqlChessman.Empty);
        }
        public JqlChessman Clone()
        {
            return new JqlChessman
            {
                ChessmanType = ChessmanType,
                Visible = Visible
            };
        }
        public Bitmap ToImage(Bitmap srcImg)
        {
            int offset = (int)ChessmanType - 1;
            return srcImg.Clone(new Rectangle(offset * JqlConsts.ChessmanWidth, 0, JqlConsts.ChessmanWidth, JqlConsts.ChessmanHeight), PixelFormat.DontCare);
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            JqlChessman chessman = obj as JqlChessman;
            if (ChessmanType != chessman.ChessmanType)
            {
                return false;
            }

            return true;
        }
        public override int GetHashCode()
        {
            return (int)ChessmanType;
        }

        public static bool operator ==(JqlChessman chessman1, JqlChessman chessman2)
        {
            if (Object.ReferenceEquals(chessman1, null) && Object.ReferenceEquals(chessman2, null)) return true;
            if (Object.ReferenceEquals(chessman1, null) || Object.ReferenceEquals(chessman2, null)) return false;

            return chessman1.Equals(chessman2);
        }
        public static bool operator !=(JqlChessman chessman1, JqlChessman chessman2)
        {
            return !(chessman1 == chessman2);
        }
    }
}
